package com.qding.park.manager.service.core.biz;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.qding.park.common.core.constant.CacheConstant;
import com.qding.park.common.core.constant.CacheKeyThree;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.manager.pojo.TbParkingrule;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.TbSupplycomany;
import com.qding.park.manager.pojo.TbUserInfo;
import com.qding.park.manager.service.core.mapper.TbParkingruleMapper;
import com.qding.park.manager.service.core.mapper.TbParkspaceMapper;
import com.qding.park.manager.service.core.mapper.TbSupplycomanyMapper;
import com.qding.park.manager.service.core.mapper.TbUserInfoMapper;

/**
 * @ClassName ParkBiz
 * @Description TODO(车场业务)
 * @author Administrator
 * @Date 2016年8月23日 下午2:08:04
 * @version 1.0.0
 */
@Component("parkBiz")
public class ParkBiz {
	private static final Log _log = LogFactory.getLog(ParkBiz.class);
	@Autowired
	private TbParkspaceMapper tbParkspaceMapper;

	@Autowired
	private TbSupplycomanyMapper tbSupplycomanyMapper;

	@Autowired
	private TbUserInfoMapper tbUserInfoMapper;
	
	/**
	  * @Fields tbParkingruleMapper : TODO（车场计费规则）
	  */
	@Autowired
	private TbParkingruleMapper tbParkingruleMapper;

	/**
	 * @Field @cacheConstant : TODO(缓存用到的一些常量)
	 */
	public CacheConstant cacheConstant;

//	@Value("${cache.park.expiredate}")
//	public int cache_park_expiredate;

	/**
	 * @Field @cacheKeyThree : TODO(缓存用到的key前缀)
	 */
	public CacheKeyThree cacheKeyThree = new CacheKeyThree();
	
	@Transactional(rollbackFor=Throwable.class) 
	public void mysql_test(){
		
		int num=tbParkingruleMapper.mysql_test();
		
		_log.info("num="+num);
		_log.info("num="+num);
	}

	/**
	 * @Description (根据用户id,与手机号查询名下所有车场信息)
	 * @param userId
	 * @param phone
	 */
	public List getParkings(String bjuserid, long phone) throws ServiceCoreException {

		// List<TbParkspace>
		// list=tbParkspaceMapper.queryParksByUserPhone(userId, phone);
		// 先查询此手机号是否已经被别的用户占用了
		int temp = tbUserInfoMapper.queryNumByPhoneNotBjuserid(bjuserid, phone);
		if (temp > 0) {
			throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_EXIST_PHONEUSER.getId(), null,
					ExceptionMessage.createExceptionMessage(
							ServiceCoreException.TypeEnum.EXCEPTION_EXIST_PHONEUSER.getDesc(), "请联络物业管理处更新登记车辆时预留的手机号",
							"手机=" + phone + " bjuserid=" + bjuserid));
		}

		// 更新用户id、北京id之间的对应关系 
		int num = tbUserInfoMapper.updateBjuseridByPhone(bjuserid, phone);
		if (num == 0) {
			throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_USER.getId(), null,
					ExceptionMessage.createExceptionMessage(
							ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_USER.getDesc(), "请联络物业管理处更新登记车辆时预留的手机号",
							"手机=" + phone + " bjuserid=" + bjuserid));
		}

		// 查询用户信息
		TbUserInfo tbUserInfo = tbUserInfoMapper.queryUserByBjUserId(bjuserid);
		int userId = tbUserInfo.getId();// 用户id
		List<TbParkspace> list = tbParkspaceMapper.queryParksByUserId(userId);

		return list;

	}
	
	
//	public  ParkDataResult getParkingsDataResult(String bjuserid, long phone){
//		
//		return null;
//	}
	
//	/**
//	 * @Description (查询车场预定规则)
//	 * @param parkId
//	 *            车场id
//	 * @return
//	 */
//	@MethodCacheable(expire = "#root.cache_park_expiredate",//过期时间
//			key = "#root.cacheKeyThree.PARKINFO+#root.cacheKeyThree.SEPARATOR+#parkId", //指定key
//			cacheSwitch = MethodCacheable.CACHESWITCH.ON,//缓存开关
//			serviceType = MethodCacheable.SERVICETYPE.BEFORE)//业务处理类型
//	public TbParkspace getparkBookRules(int parkId) {
//		TbParkspace tbParkSpace = tbParkspaceMapper.queryBookRulesById(parkId);
//		return tbParkSpace;
//
//	}
	
//	@MethodCacheable(expire = "#root.cache_park_expiredate",//过期时间
//			key = "#root.cacheKeyThree.PARKINFO+#root.cacheKeyThree.SEPARATOR+#parkId", //指定key
//			cacheSwitch = MethodCacheable.CACHESWITCH.ON,//缓存开关
//			serviceType = MethodCacheable.SERVICETYPE.BEFORE)//业务处理类型
	public TbParkspace getParkInfoByParkId(int parkId) {
		TbParkspace tbParkSpace = tbParkspaceMapper.queryParkInfoByParkId(parkId);
		return tbParkSpace;
	}
	
	
	
	public TbParkspace queryParkInfoByCodeSupplier(String parkCode, int supplierId) {
		TbParkspace tbParkSpace = tbParkspaceMapper.queryParkInfoByCodeSupplier(parkCode, supplierId);
		return tbParkSpace;

	}

	public List<TbSupplycomany> queryRegisterSupplyByCarnum(String carNum) {
		List<TbSupplycomany> supplycomanys = tbSupplycomanyMapper.queryRegisterSupplyByCarnum(carNum);

		return supplycomanys;
	}
	
	
	/**
	  * syncSave(同步保存车场计费规则)
	  * TODO(这里描述这个方法适用条件 – 可选)
	  * TODO(这里描述这个方法的执行流程 – 可选)
	  * TODO(这里描述这个方法的使用方法 – 可选)
	  * TODO(这里描述这个方法的注意事项 – 可选)
	  *
	  * @Title: syncSave
	  * @Description: TODO
	  * @param @param tbParkRulelist
	  * @param @param park    设定文件
	  * @return void    返回类型
	  * @throws
	  */
	
	@Transactional(rollbackFor=Exception.class) 
	public void syncSave(List<TbParkingrule> tbParkRulelist,TbParkspace park){
		int parkid=park.getId();
		int delNum=tbParkingruleMapper.deleteByParkid(parkid);
		
		_log.info("delete num="+delNum);
		
		
		tbParkingruleMapper.batchInsert(tbParkRulelist);
	}
	

}
